﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using PublicClass;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublicTuKu
{
    public class SampleJigEnts : DrawJig
    {
        public List<Entity> EntList { get; }
        private Point3d mPtDrag = new Point3d();
        public Point3d DragPt
        {
            get
            {
                return mPtDrag;
            }
            set
            {
                UpData(value);
            }
        }
        public SampleJigEnts()
        {
            EntList = new List<Entity>();
            mPtDrag = Point3d.Origin;
        }

        void UpData(Point3d ptNew)
        {
            Vector3d vec = ptNew - mPtDrag;
            Matrix3d mat = Matrix3d.Displacement(vec);
            foreach (var item in EntList)
            {
                item.TransformBy(mat);
            }
            mPtDrag = ptNew;
        }

        /// <summary>
        /// 所有实体加入数据库
        /// </summary>
        public void AddToDb()
        {
            using (CadUnits.CurDoc().LockDocument())
            {
                using (Transaction ts = CadUnits.StartTransaction())
                {
                    foreach (var item in EntList)
                    {
                        CadUnits.AppendEntity(ts, item);
                    }

                    ts.Commit();
                }
            }

        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions jigPrompt = null;
            jigPrompt = new JigPromptPointOptions("\n指定插入点");

            jigPrompt.UserInputControls = UserInputControls.GovernedByOrthoMode | UserInputControls.NullResponseAccepted | UserInputControls.DoNotEchoCancelForCtrlC;

            // setUserInputControls((UserInputControls)(kGovernedByOrthoMode | kNullResponseAccepted | kDontEchoCancelForCtrlC));

            PromptPointResult res = prompts.AcquirePoint(jigPrompt);

            if (res.Status != PromptStatus.OK)
                return SamplerStatus.NoChange;

            Point3d ptLast = DragPt;

            if (ptLast.IsEqualTo(res.Value))
                return SamplerStatus.NoChange;

            DragPt = res.Value;

            return SamplerStatus.OK;
        }

        protected override bool WorldDraw(WorldDraw draw)
        {
            foreach (var item in EntList)
            {
                item.WorldDraw(draw);
            }

            return true;
        }
    }
}
